<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 15-4-27
 * Time: 下午3:01
 */

class service extends adminBase{
    function __construct(){
        parent::__construct();
        $this->cms->setPageTitle("管理面板");
        $this->cms->setActionTitle("服务站管理");
        $this->cms->setControlFile("admin/tpl/service/service_control.json");
        $admin_info=$this->user->getLoginInfo();
        $user=UserMode::init();
        $levels=$user->getLevels($admin_info['id']);
        if(!$user->checkLevels(4,$levels)){
            $r['message']="您不具有此权限！";
            View::displayAsHtml($r,"plugin/state/tips.php");
            exit();
        }
    }

    function indexTask($page=1,$header_name,$header_tel,$service_name,$type){
        $url=WebRouter::init()->getQuestion("page=");
        $page=$page>=1? $page: 1;
        $countOfPage=20;
        $offset=($page-1)*$countOfPage;
        $service=ServiceMode::init();
        $address=AddressMode::init();
        if(!isset($header_name)&&!isset($header_tel)&&!isset($service_name)){
            if($type=='checked'){
                list($r['service_info'],$service_total)=$service->getList(1,$countOfPage,$offset);
            }else if($type=='uncheck'){
                list($r['service_info'],$service_total)=$service->getList(0,$countOfPage,$offset);
            }else{
                list($r['service_info'],$service_total)=$service->getList(2,$countOfPage,$offset);
            }
            usort($r['service_info'],function($a,$b){
                return ord(substr(iconv('utf-8', 'gbk', $a['service_name']), 0, 1))-
                ord(substr(iconv('utf-8', 'gbk', $b['service_name']), 0, 1));
            });
            $r['service_info']=is_array($r['service_info'])?$r['service_info']:array();
            foreach($r['service_info'] as &$v){
                $service_address=$address->getDetailAsString($v['address_id']);
                $v['address_id']=$service_address;
            }
            $r['pages']=new Page($page,$service_total,$countOfPage);
            $r['pages']->setPageCallback(create_function('$page','return "'.$url.'".$page;'));
        }else {
            $r['search']=$service->getServiceInfoBySearch($header_name,$header_tel,$service_name);
            if(!empty($r['search'])){
                foreach($r['search'] as &$v){
                    $v['address_id']=$address->getDetailAsString($v['address_id']);
                }
            }
        }
        $this->cms->tableScene($r,"admin/tpl/service/service.php");
    }

    function checkPassTask($page,$id){
        $service=ServiceMode::init();
        if($service->auth($id)){
            $r['url']=WebRouter::init()->getAction("index");
            $r['message']="审核通过";
        }else{
            $r['message']="审核通过失败，请重试";
        }
        View::displayAsHtml($r,"plugin/state/tips.php");
    }

    function checkCancelTask($id){
        $service=ServiceMode::init();
        if($service->auth($id,0)){
            $r['url']=WebRouter::init()->getAction("index");
            $r['message']="冻结成功";
        }else{
            $r['message']="冻结失败，请重试";
        }
        View::displayAsHtml($r,"plugin/state/tips.php");
    }

    function serviceModifyTask($id){
        $service=ServiceMode::init();
        $r=$service->getDetail($id);
        $address=AddressMode::init();
        $r['service_address']=$address->getDetailAsString($r['address_id']);
        $pic=PictureModule::init("picture");
        $facade_pic=$pic->getDetail($r['facade_pic']);
        $r['facade_pic']=!empty($facade_pic)?$facade_pic:'upload/thumb.jpg';
        $idnum_pic=$pic->getDetail($r['facade_pic']);
        $r['idnum_pic']=!empty($idnum_pic)?$idnum_pic:'upload/thumb.jpg';
        $this->cms->formScene($r,"admin/tpl/service/serviceModify.php");
    }

    function serviceModifySubmitTask($id,$real_name,$sex,$birth,$academic,$idnum,$idnum_pic,$header_name,
                                     $header_tel, $service_name,$facade_pic,$nation, $tel, $email,
                                     $address, $resume,$computer_skill,$address_id, $address_text,
                                     $latitude,$longitude){
        $service=ServiceMode::init();
        $s=$service->modify($id,$real_name,$sex,$birth,$academic,$idnum,$idnum_pic,$header_name,
            $header_tel, $service_name,$facade_pic,$nation, $tel, $email,
            $address, $resume,$computer_skill,$address_id, $address_text,$latitude,$longitude);
        if($s){
            $r['url']=WebRouter::init()->getAction("index");
            $r['message']="服务站信息修改成功";
        }else{
            $r['message']="修改失败，请重试";
        }
        View::displayAsHtml($r,"plugin/state/tips.php");
    }


//    function serviceAddSubmitTask($real_name,$sex,$birth,$academic,$idnum,$header_name,
//                                  $header_tel,$service_name,$nation,$tel,$email,$address,
//                                  $resume,$computer_skill,$address_id,$address_text,$latitude,$longitude){
//        $service=ServiceMode::init();
//        $up=Uploader::init();
//        $pic=PictureModule::init("picture");
//        $picArray_facade=array( array('set'=>"facade_pic","width"=>800,'height'=>600) );
//        list($stateInfo_facade,$picDataArray_facade)=$up->upImage('facade_pic',null,$picArray_facade);
//        $picArray_idum=array( array('set'=>"idnum_pic","width"=>800,'height'=>600) );
//        list($stateInfo_idum,$picDataArray_idum)=$up->upImage('idnum_pic',null,$picArray_idum);
//        if((strtolower($stateInfo_facade)=='success')&&(strtolower($stateInfo_idum)=='success')){
//            foreach($picDataArray_facade as &$picData){
//                $picData['photoId']=$pic->add($picData['url'],$picData['savePath'],$picData['filename']);
//            }
//            foreach($picDataArray_idum as &$picData){
//                $picData['photoId']=$pic->add($picData['url'],$picData['savePath'],$picData['filename']);
//            }
//            $service->add(null,$real_name,$sex,$birth,$academic,$idnum,$picDataArray_idum['idnum_pic']['photoId'],$header_name,
//                    $header_tel, $service_name,$picDataArray_facade['facade_pic']['photoId'],$nation, $tel, $email,
//                    $address, $resume,$computer_skill,$address_id, $address_text,
//                    $latitude,$longitude);
//            $r['url']=WebRouter::init()->getAction("index");
//            $r['message']="服务站添加成功";
//        }else{
//            $r['message']="添加失败，请重试";
//        }
//        View::displayAsHtml($r,"plugin/state/tips.php");
//    }

    function serviceDeleteTask($id){
        $service=ServiceMode::init();
        $user=UserModule::init('user');
        $shop=ShopMode::init();
        $user_info=$service->getDetail($id);
        $shop_id=$shop->getShopId($user_info['user_id']);
        if(isset($shop_id)){
            $user->setType($user_info['user_id'],'shop');
        }else{
            $user->setType($user_info['user_id'],'user');
        }
        if($service->delete($id)){
            $r['url']=WebRouter::init()->getAction("index");
            $r['message']="删除成功";
        }else{
            $r['message']="删除失败，请重试";
        }
        View::displayAsHtml($r,"plugin/state/tips.php");
    }
} 